package com.onstar.cn.framework.mvp.base.core;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.mvp.base.IMVPPresenter;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.network.entity.RequestEntity;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;
import rx.subscriptions.Subscriptions;

/**
 * Presenter处理基类,可继承，也可以自己实现IMVPPresenter处理自己业务逻辑
 * Created by 7828wc on 2016/6/21.
 */
public abstract class BasePresenter<T> implements IMVPPresenter<T> {
    protected Subscription subscription = Subscriptions.empty();
    protected BaseService baseService;
    protected IMVPScheduler mvpScheduler;
    protected IMVPView view;
    protected RequestEntity requestEntity;
    protected CompositeSubscription mCompositeSubscription;

    public void addSubscription(Subscription s) {
        if (this.mCompositeSubscription == null) {
            this.mCompositeSubscription = new CompositeSubscription();
        }
        this.mCompositeSubscription.add(s);
    }

//    public BasePresenter(BaseService baseService, IMVPScheduler mvpScheduler) {
//        super();
//        this.baseService = baseService;
//        this.mvpScheduler = mvpScheduler;
//    }

    public void setServiceProvider(BaseService baseService) {
        this.baseService = baseService;
    }

    public BasePresenter(IMVPScheduler mvpScheduler) {
        super();
        this.mvpScheduler = mvpScheduler;
    }

    @Override
    public void setView(IMVPView<T> view) {
        this.view = view;
    }

    /**
     * 如调用框架内start方法，需注意在persenter中设置setServiceProvider，否则回抛出Runtime NullPointException
     */
    @Override
    public void start() {
        subscription = getObservable().subscribe(getSubscriber());
    }

    private Subscriber<T> getSubscriber() {
        return new Subscriber<T>() {
            @Override
            public void onStart() {
                super.onStart();
                view.setLoading(true);
            }

            @Override
            public void onCompleted() {
                view.setLoading(false);
            }

            @Override
            public void onError(Throwable e) {
                view.setLoading(false);
                if (e instanceof OnStarException) {
                    view.onResponseError((OnStarException) e);
                }
            }

            @Override
            public void onNext(T t) {
                //返回做请求的数据给view对象
                view.onReponseSuccess(t);
            }
        };
    }

    private Observable<T> getObservable() {
        if (baseService != null) {
            return baseService.processDatas(requestEntity)
                    .subscribeOn(mvpScheduler.backgroundThread())
                    .observeOn(mvpScheduler.mainThread());
        } else {
            return baseService.showDatas()
                    .subscribeOn(mvpScheduler.backgroundThread())
                    .observeOn(mvpScheduler.mainThread());
        }
    }

    @Override
    public void finish() {
        if (subscription != null && !subscription.isUnsubscribed()) {
            subscription.unsubscribe();
        }
        if (this.mCompositeSubscription != null) {
            this.mCompositeSubscription.unsubscribe();
        }
    }
}
